

#include "cybertron/network/SocketEventHandler.hpp"
#include "cybertron/network/SocketBase.hpp"

CYBERTRON_BEGIN

CybertronCoreAPI SocketEventHandler::SocketEventHandler()
{
	mDisposed = 0;
}

CybertronCoreAPI SocketEventHandler::~SocketEventHandler()
{
}

CybertronCoreAPI void SocketEventHandler::send(const Message& msg)
{
	auto pSocket = getSocket();
	if (pSocket != nullptr)
	{
		pSocket->send(msg);
	}
}

CybertronCoreAPI void SocketEventHandler::send(
	std::uint16_t msgId,
	const google::protobuf::MessageLite& msg)
{
	auto pSocket = getSocket();
	if (pSocket != nullptr)
	{
		pSocket->send(msgId, msg);
	}
}

CybertronCoreAPI void SocketEventHandler::sendHeartbeat()
{
	auto pSocket = getSocket();
	if (pSocket != nullptr)
	{
		pSocket->sendHeartbeat();
	}
}

CybertronCoreAPI void SocketEventHandler::close(const char* reason)
{
	auto pSocket = getSocket();

	if (pSocket != nullptr)
	{
		pSocket->close(reason);
	}
}

CybertronCoreAPI void SocketEventHandler::setDisposed()
{
	if (mDisposed++ == 0)
	{
		onDisposed();
	}
}

CybertronCoreAPI bool SocketEventHandler::getDisposed() const
{
	return (mDisposed != 0);
}

CybertronCoreAPI void SocketEventHandler::onDisposed()
{
}

CybertronCoreAPI void SocketEventHandler::onDisconnected()
{
}

CybertronCoreAPI void SocketEventHandler::onConnected()
{
}

CybertronCoreAPI void SocketEventHandler::onMessage(Message& msg)
{
}

CybertronCoreAPI void SocketEventHandler::setSocket(std::weak_ptr<SocketBase> pSocket)
{
	mSocket = pSocket;
}

CybertronCoreAPI std::shared_ptr<SocketBase> SocketEventHandler::getSocket() const
{
	return mSocket.lock();
}

CYBERTRON_END
